package config

import (
	"context"
	"fmt"


	"karnin-zero-admin/common/errorx"
	"karnin-zero-admin/service/sys/model"

	"karnin-zero-admin/service/sys/api/internal/svc"
	"karnin-zero-admin/service/sys/api/internal/types"

	"github.com/tal-tech/go-zero/core/logx"
)

type ConfigCreateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewConfigCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) ConfigCreateLogic {
	return ConfigCreateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}
type User struct {
	FirstName      string     `validate:"required"`
	LastName       string     `validate:"required"`
	Age            uint8      `validate:"gte=0,lte=130"`
	Email          string     `validate:"required,email"`
	FavouriteColor string     `validate:"iscolor"`                // alias for 'hexcolor|rgb|rgba|hsl|hsla'
}
func (l *ConfigCreateLogic) ConfigCreate(req types.ConfigCreateRequest) (resp *types.ConfigCreateResponse, err error) {



	//验证是否存在
	var existConfig model.Config
	l.svcCtx.GormDB.Where("`key` = ?", req.Key).First(&existConfig)
	if existConfig.ID != 0 {
		return &types.ConfigCreateResponse{}, errorx.ConfigExist
	}
	//创建
	config := model.Config{
		Key:     req.Key,
		Value:   req.Value,
		Comment: req.Comment,
	}
	result := l.svcCtx.GormDB.Create(&config)
	if result.Error != nil {
		fmt.Println(111)
	}
	return &types.ConfigCreateResponse{}, nil
}
